package com.zhike.b2b.web;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Service;

import com.saas.sys.bpm.BpmDefinitionDAO;
import com.saas.sys.bpm.TradeCheckIntf;
import com.saas.sys.bpm.TradeTypeDAO;
import com.saas.sys.buffer.Buffers;
import com.saas.sys.log.Logger;

@Service("checktrade")
public class TradeCheck implements TradeCheckIntf
{
  Logger log = new Logger(this);
  Buffers outBuffer = new Buffers();

  public void execTradeCheck(Buffers paramBuffers)
  {
    this.log.LOG_INFO("进入主控业务方法...");
    if (paramBuffers.getString("TRADE_TYPE_CODE") == "")
      throw new RuntimeException("业务类型域字段未赋值！");
    outBuffer = paramBuffers;
    log.LOG_INFO("********获取业务类型参数开始*********************");
    TradeTypeDAO ttd = new TradeTypeDAO();
    ttd.setParam(":VTRADETYPECODE", "0900");
    ttd = ttd.selByInfo("SEL_BY_PK");
    if (ttd == null)
      throw new RuntimeException("校验类业务类型未定义【0900】！");
    log.LOG_INFO("********获取业务类型参数结束*********************");
    log.LOG_INFO("********获取业务流程定义开始*********************");
    BpmDefinitionDAO bdd = new BpmDefinitionDAO();
    bdd.setParam(":VTRADETYPECODE", "0900");
    List bpmDef = bdd.selByList("SEL_BY_PK");
    int i = 0;
    if (bpmDef == null)
      throw new RuntimeException("业务流程未定义！");
    this.log.LOG_INFO("********获取业务流程定义结束*********************");
    try
    {
      String str = "";
      Iterator localIterator = bpmDef.iterator();
      while (localIterator.hasNext())
      {
        HashMap localHashMap = (HashMap)localIterator.next();
        Class localClass = Class.forName(localHashMap.get("node_class").toString());
        Object obj = localClass.newInstance();
        this.log.LOG_INFO("********输入参数开始*********************");
        paramBuffers.list();
        this.log.LOG_INFO("********输入参数结束*********************");
        Method localMethod = obj.getClass().getDeclaredMethod(localHashMap.get("node_method").toString(), new Class[] { Buffers.class });
        this.log.LOG_INFO("********[开始执行原子业务方法<" + localMethod.getName() + ">]*********************");
        localMethod.invoke(obj, new Object[] { paramBuffers });
        this.log.LOG_INFO("********原子业务方法执行完毕*********************");
        str = localClass.getDeclaredField(localHashMap.get("out_query").toString()).getName();
        this.log.LOG_INFO("********<" + localHashMap.get("out_query").toString() + ">*********************");
        if (localHashMap.get("rsrv_str5").toString().equalsIgnoreCase("1"))
        {
          str = localClass.getDeclaredField(localHashMap.get("out_buffer").toString().trim()).getName();
          outBuffer = ((Buffers)PropertyUtils.getProperty(obj, str));
        }
        else
        {
          str = localClass.getDeclaredField(localHashMap.get("out_buffer").toString().trim()).getName();
          outBuffer = ((Buffers)PropertyUtils.getProperty(obj, str));
        }
        i += 1;
        this.log.LOG_INFO("********获取原子业务方法输出ArrayList*********************");
        this.log.LOG_INFO("********[结束执行原子业务方法<" + localMethod.getName() + ">]*********************");
      }
      if (outBuffer.getInt("RESULT_CODE") == 0)
          outBuffer.setString("SUCCEED_FWD", ttd.getSucceed_fwd());
      else
          outBuffer.setString("ERROR_FWD", ttd.getError_fwd());
      this.log.LOG_INFO("--------[全部业务流程处理完毕!]--------");
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new RuntimeException("调用类未找到：" + localClassNotFoundException);
    }
    catch (InstantiationException localInstantiationException)
    {
      throw new RuntimeException(localInstantiationException);
    }
    catch (IllegalAccessException localIllegalAccessException)
    {
      throw new RuntimeException(localIllegalAccessException);
    }
    catch (InvocationTargetException localInvocationTargetException)
    {
      throw new RuntimeException(localInvocationTargetException.getTargetException());
    }
    catch (NoSuchMethodException localNoSuchMethodException)
    {
      throw new RuntimeException("调用方法未找到：" + localNoSuchMethodException);
    }
    catch (NoSuchFieldException localNoSuchFieldException)
    {
      throw new RuntimeException("JAVABEAN的域字段未找到：" + localNoSuchFieldException);
    }
    log.LOG_INFO("退出主控业务方法...");
  }

  public Buffers getOutBuffer()
  {
    return outBuffer;
  }
}

